package device

import (
	"fmt"
	"goskeleton/app/core/container"
	"goskeleton/app/global/consts"
	"goskeleton/app/http/controller/api/device"
	"goskeleton/app/http/validator/core/data_transfer"
	"goskeleton/app/utils/response"

	"github.com/gin-gonic/gin"
)

type List struct {
	Page     int    `form:"page" json:"page" binding:"required,min=1"`
	PageSize int    `form:"page_size" json:"page_size" binding:"required,min=1,max=100"`
	Keyword  string `form:"keyword" json:"keyword"`
	Platform int16  `form:"platform" json:"platform"`
}

func (l List) CheckParams(context *gin.Context) {
	if err := context.ShouldBind(&l); err != nil {
		response.ValidatorError(context, err)
		return
	}

	extraAddBindDataContext := data_transfer.DataAddContext(l, consts.ValidatorPrefix, context)
	if extraAddBindDataContext == nil {
		response.ErrorSystem(context, "DeviceList表单验证器json化失败", "")
		return
	}

	deviceHandler := &device.Device{}
	deviceHandler.List(extraAddBindDataContext)
}

type Create struct {
	Cpu          string `form:"cpu" json:"cpu" binding:"required"`
	ImageUrl     string `form:"image_url" json:"image_url" binding:"required"`
	Manufacturer string `form:"manufacturer" json:"manufacturer" binding:"required"`
	ManSize      uint   `form:"man_size" json:"man_size" binding:"required"`
	Model        string `form:"model" json:"model" binding:"required"`
	Name         string `form:"name" json:"name" binding:"required"`
	Password     string `form:"password" json:"password"`
	Platform     int16  `form:"platform" json:"platform"`
	Size         string `form:"size" json:"size" binding:"required"`
	UdId         string `form:"ud_id" json:"ud_id" binding:"required"`
	Version      string `form:"version" json:"version" binding:"required"`
	NickName     string `form:"nick_name" json:"nick_name"`
	IsHm         int16  `form:"is_hm" json:"is_hm"`
	Qrcode       string `form:"qrcode" json:"qrcode"`
	IsShow       int8   `form:"is_show" json:"is_show"`
	Orders       int    `form:"orders" json:"orders"`
}

func (c Create) CheckParams(context *gin.Context) {
	if err := context.ShouldBind(&c); err != nil {
		fmt.Println("创建设备参数验证失败：", err)
		response.ValidatorError(context, err)
		return
	}
	extraAddBindDataContext := data_transfer.DataAddContext(c, consts.ValidatorPrefix, context)
	if extraAddBindDataContext == nil {
		response.ErrorSystem(context, "DeviceCreate表单验证器json化失败", "")
		return
	}
	deviceHandler := &device.Device{}
	deviceHandler.Create(extraAddBindDataContext)
}

type Update struct {
	Id          uint   `form:"id" json:"id" binding:"required,min=1"`
	Name        string `form:"name" json:"name" binding:"required,min=1,max=50"`
	Description string `form:"description" json:"description" binding:"max=200"`
}

func (u Update) CheckParams(context *gin.Context) {
	if err := context.ShouldBind(&u); err != nil {
		response.ValidatorError(context, err)
		return
	}

	extraAddBindDataContext := data_transfer.DataAddContext(u, consts.ValidatorPrefix, context)
	if extraAddBindDataContext == nil {
		response.ErrorSystem(context, "DeviceUpdate表单验证器json化失败", "")
		return
	}

	deviceHandler := &device.Device{}
	deviceHandler.Update(extraAddBindDataContext)
}

type Delete struct {
	Id uint `form:"id" json:"id" binding:"required,min=1"`
}

func (d Delete) CheckParams(context *gin.Context) {
	if err := context.ShouldBind(&d); err != nil {
		response.ValidatorError(context, err)
		return
	}

	extraAddBindDataContext := data_transfer.DataAddContext(d, consts.ValidatorPrefix, context)
	if extraAddBindDataContext == nil {
		response.ErrorSystem(context, "DeviceDelete表单验证器json化失败", "")
		return
	}

	deviceHandler := &device.Device{}
	deviceHandler.Delete(extraAddBindDataContext)
}

type Detail struct {
	Id uint `form:"id" json:"id" binding:"required,min=1"`
}

func (d Detail) CheckParams(context *gin.Context) {
	if err := context.ShouldBind(&d); err != nil {
		response.ValidatorError(context, err)
		return
	}

	extraAddBindDataContext := data_transfer.DataAddContext(d, consts.ValidatorPrefix, context)
	if extraAddBindDataContext == nil {
		response.ErrorSystem(context, "DeviceDetail表单验证器json化失败", "")
		return
	}

	deviceHandler := &device.Device{}
	deviceHandler.Detail(extraAddBindDataContext)
}

// 注册验证器
func RegisterValidator() {
	containers := container.CreateContainersFactory()
	containers.Set(consts.ValidatorPrefix+"DeviceList", &List{})
	containers.Set(consts.ValidatorPrefix+"DeviceCreate", &Create{})
	containers.Set(consts.ValidatorPrefix+"DeviceUpdate", &Update{})
	containers.Set(consts.ValidatorPrefix+"DeviceDelete", &Delete{})
	containers.Set(consts.ValidatorPrefix+"DeviceDetail", &Detail{})
}
